// Helpers for resolving media URLs (avatars, images) consistently across the app
// - If the value is an absolute URL (http/https), return as-is
// - If the value is a relative path (e.g. /avatar/user_3/xxx.jpg), prefix with VUE_APP_BASE_API
// - If empty/undefined, return empty string (no default fallback)
import { getEnv } from './env.js'

export const resolveMediaUrl = (url) => {
  if (!url) return ''
  if (typeof url !== 'string') return ''
  if (url.startsWith('http')) return url
  const base = getEnv('VUE_APP_BASE_API', '')
  const path = url.startsWith('/') ? url : `/${url}`
  return base ? `${base}${path}` : path
}

export const resolveAvatarUrl = (url) => resolveMediaUrl(url)

export default {
  resolveMediaUrl,
  resolveAvatarUrl,
}

